#include <netif/grethif.h>
#include <lwip/netif.h>
#include <ipv4/lwip/ip_addr.h>
#include <ipv4/lwip/ip.h>
#include <ipv4/lwip/icmp.h>

static struct netif s_netif;
static struct ip_addr s_ipaddr;
static struct ip_addr s_netmask;
static struct ip_addr s_gateway;


static err_t 
ethinput (struct pbuf *p, struct netif *netif)
{
    struct ip_hdr* ipheader = (struct ip_hdr*)p->payload;
    switch(IPH_PROTO(ipheader))
    {
        case IP_PROTO_ICMP:
            printf("icmp\n");
            icmp_input(p,netif);
    }
    pbuf_free(p);
    p=NULL;
    
    return ERR_OK;
}

int main(void)
{
    // Set Hardware address
    s_netif.hwaddr[0] = 0xAA;
    s_netif.hwaddr[1] = 0xAB;
    s_netif.hwaddr[2] = 0xAC;
    s_netif.hwaddr[3] = 0xAD;
    s_netif.hwaddr[4] = 0xAE;
    s_netif.hwaddr[5] = 0xAF;
    
    IP4_ADDR(&s_ipaddr, 192,168,4,4);
    IP4_ADDR(&s_gateway, 192,168,4,1);
    IP4_ADDR(&s_netmask, 255,255,255,0);
    netif_init();
    ip_init();
    struct netif* pnetif = netif_add(&s_netif, &s_ipaddr, &s_netmask, &s_gateway, NULL, grethif_init, ethinput);
    if(pnetif == NULL)
    {
        printf("pnetif null\n");
        return -1;
    }
    
//     netif_set_default(&s_netif);
//     netif_set_up(&s_netif);
    static int delay;
    while(1)
    {
        grethif_poll_in(&s_netif);
        delay = 10000000;
        while(--delay);
    }
    return 0;
}

